package map.android.baidu.carpool.utils;


import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;

public class ShortcutUtils {
//private static final String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
    
    private static final String PERMISSION_WRITE = "com.android.launcher.permission.WRITE_SETTINGS";
    private static final String PERMISSION_READ = "com.android.launcher.permission.READ_SETTINGS";

    private static final String TABLE_FAVORITES = "favorites";
    private static final String PARAMETER_NOTIFY = "notify";
//    private static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";

/*	public static void addComponentShortcut(String category, String name, int id) {
		if (TextUtils.isEmpty(name) || id <= 0) {
			return;
		}
		
		Context context = APIProxy.sys().getApplicationContext();
        
        Intent targetIntent = null;
        
        String url = "component?comName=" + category;
        try {
        	targetIntent = Intent.getIntent("intent://map/" + url + "#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
        }catch (URISyntaxException e) {
        	e.printStackTrace();
        }
        
        
        Intent shortcut = new Intent(ACTION_INSTALL_SHORTCUT);
        
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);
        
        
        
        
        if (shortcutExists(context, name, targetIntent)) {
            return;
        }
        // 快捷方式名称
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
        // 不允许重复创建
        shortcut.putExtra(EXTRA_SHORTCUT_DUPLICATE, false);
        // 快捷方式的图标
        
        Parcelable iconResource = null;
        iconResource = Intent.ShortcutIconResource.fromContext(context, id);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
        context.sendBroadcast(shortcut);
	}*/
	
	
    public static boolean shortcutExists(Context context, String title, Intent intent) {
        final Uri contentUri = getLauncherFavoritesContentUri(context);
        if (contentUri == null)
            return false;
        final ContentResolver cr = context.getContentResolver();
        boolean result = false;
        try {
            Cursor c = cr.query(contentUri, new String[] { "title", "iconResource" }, "title=?",
                    new String[] { title }, null);
            if (c != null) {
                try {
                    result = c.moveToFirst();
                } finally {
                    c.close();
                }
            }
        } catch (Exception e) {

        }
        return result;
    }
    
    private static Uri getLauncherFavoritesContentUri(Context ctx) {
        String authority = null;
        authority = getLauncherAuthority(ctx);
        if (authority == null)
            return null;
        return Uri.parse("content://" + authority + "/" + TABLE_FAVORITES + "?" + PARAMETER_NOTIFY + "=true");
    }
    
    /**
     * 提取默认设置应用中有特定权限的provider authority
     * 
     * @param context
     * @param
     * @return 如果没有默认应用,返回null
     */
    private static String getLauncherAuthority(Context context) {

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        ResolveInfo resolveInfo = context.getPackageManager()
                .resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
        if (resolveInfo == null)
            return null;
        String currentHomePackage = resolveInfo.activityInfo.packageName;
        if (currentHomePackage == null || currentHomePackage.equals(""))
            return null;
        try {
            PackageInfo pkg = context.getPackageManager().getPackageInfo(currentHomePackage,
                    PackageManager.GET_PROVIDERS);
            if (pkg != null) {
                ProviderInfo[] providers = pkg.providers;
                if (providers != null) {
                    for (ProviderInfo provider : providers) {
                        if (PERMISSION_READ.equals(provider.readPermission))
                            return provider.authority;
                        if (PERMISSION_WRITE.equals(provider.writePermission))
                            return provider.authority;
                    }
                }
            }
        } catch (NameNotFoundException e) {
            return null;
        }

        return null;
    }
    
}
